package cn.com.dhc.danlu.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.util.AttributeSet;
import android.webkit.WebSettings;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.io.File;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.widget.dialog.ViewDialog;

/**
 * @author wuhaiyang
 */
public class CacheWebView extends WebView {
    public static final String WEBVIEW_CACHE_DRINAME = "superwebviewcache";

    private OnWebViewClient mCallback;

    public CacheWebView (Context context) {
        this (context, null);
    }

    public CacheWebView (Context context, AttributeSet attrs) {
        this (context, attrs, 0);
    }

    public CacheWebView (Context context, AttributeSet attrs, int defStyleAttr) {
        super (context, attrs, defStyleAttr);

        if (isInEditMode ()) {
            return;
        }
        setAttr ();
        bindAction ();
    }

    @SuppressLint ("SetJavaScriptEnabled")
    private void setAttr () {
        WebSettings settings = getSettings ();
        settings.setJavaScriptEnabled (true);
        settings.setRenderPriority (RenderPriority.HIGH);
        settings.setPluginState (WebSettings.PluginState.ON);
        settings.setUseWideViewPort (true);
        settings.setLoadWithOverviewMode (true);
        //开启dom stroage api功能
        settings.setDomStorageEnabled (true);
        //开启 database storage API 功能
        settings.setDatabaseEnabled (true);
        settings.setCacheMode (WebSettings.LOAD_DEFAULT);
        settings.setAppCacheEnabled (true);
        //设置对应的缓存路径
        String webCacheDir = getContext ().getCacheDir ().getAbsolutePath () + File.separator + WEBVIEW_CACHE_DRINAME;
        //设置数据库缓存路径
        settings.setDatabasePath (webCacheDir);
        //设置  Application Caches 缓存目录
        settings.setAppCachePath (webCacheDir);
    }

    private void bindAction () {
        setWebViewClient (new WebViewClient () {
            @Override
            public boolean shouldOverrideUrlLoading (WebView view, String url) {
                if (url.startsWith ("tel:")) {
                    new ViewDialog.Builder (getContext ()).setMessage ("400-011-6666").setNegativeButton (R.string.btn_cancel, new DialogInterface.OnClickListener () {
                        @Override
                        public void onClick (DialogInterface dialog, int which) {
                            dialog.dismiss ();
                        }
                    }).setPositiveButton (R.string.btn_confirm, new DialogInterface.OnClickListener () {
                        @Override
                        public void onClick (DialogInterface dialog, int which) {
                            call ();
                            dialog.dismiss ();
                        }

                        private void call () {
                            String phone = "400-011-6666";
                            Intent intent = new Intent (Intent.ACTION_CALL, Uri.parse ("tel:" + phone.trim ()));
                            getContext ().startActivity (intent);
                        }
                    }).create ().show ();
                    view.reload ();
                    return true;
                }
                view.loadUrl (url);
                return true;
            }

            @Override
            public void onPageFinished (WebView view, String url) {
                super.onPageFinished (view, url);
            }

            @Override
            public void onReceivedError (WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError (view, errorCode, description, failingUrl);
                //加载本地html
                if (null != mCallback) {
                    mCallback.onReceivedError (view, errorCode, description, failingUrl);
                }
            }
        });
    }

    public void setCustomeWebViewClient (OnWebViewClient client) {
        this.mCallback = client;
    }

    public interface OnWebViewClient {
        void onReceivedError (WebView view, int errorCode, String description, String failingUrl);
    }
}